﻿using DH.Core.Logger;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Logging;
using NLog.Extensions.Logging;
using System;
using System.Collections.Generic;
using System.IO;
using System.Text;

namespace DH.Core.DI
{
  public  class MicrosoftDI
    {
        public static IServiceProvider BuildDi()
        {
            var services = new ServiceCollection();

            //Runner is the custom class
            services.AddTransient<LoggerRunner>();

            services.AddSingleton<ILoggerFactory, LoggerFactory>();
            services.AddSingleton(typeof(ILogger<>), typeof(Logger<>));
            services.AddLogging((builder) => builder.SetMinimumLevel(LogLevel.Trace));

            var serviceProvider = services.BuildServiceProvider();

            var loggerFactory = serviceProvider.GetRequiredService<ILoggerFactory>();

            //configure NLog
            //NLog.Extensions.Logging(1.0.0-rtm-rc2)
            loggerFactory.AddNLog();
            var basePath = Directory.GetCurrentDirectory();
            loggerFactory.ConfigureNLog(Path.Combine(basePath+"/config", "nlog.config"));

            return serviceProvider;
        }
    }
}
